<?php

class App
{
	public static $Current;
	public $Path;
	public $Modules = array( );
	
	// creates a new app, loads the modules
	public function App($Path)
	{
		self::$Current = $this;
		
		$this->Path = $Path;

		// Create default module for app
		$Module = new Module( '/', $Path );
		$Module->Activate( );
		
		// Load custom modules
		require_once $Path . '/Modules/start.php';
	}
	
	// load a class file from one of the modules
	public function Load($Class)
	{
		foreach($this->Modules as $Module)
			if($Module->Load($Class))
				return true;
		return false;
	}
	
	public function Init( )
	{
		if( file_exists($this->Path . '/Config.php'))
		{
			require_once $this->Path . '/Config.php';
			require_once $this->Path . '/Boot/Start.php';
		}
		else
		{
			require_once $this->Path . '/Install/Start.php';
			exit;
		}
	}
	
	public function ExecPage($Path = -1 )
	{
		// route the current query url 
		$router = new Router( $this->Path . '/WWW' );
		$router->Route( $Path );
	}
	
	public function Run($Path = -1 )
	{
		try
		{
			$this->Init();
			$this->ExecPage($Path);
		}
		catch(Exception $e)
		{
			?>We're very sorry there is an error in our website. We are trying to fix it as soon as possible.<?php
			if(get('wargarbl')==1)
				print $e; 
			//Mail::Send('Martien de Jong<martiendejong2008@gmail.com>', 'SocioRating error', $e->ToString());
		}
	}
}